<?php

namespace app\modules\Order\controllers;

use Yii;
use app\modules\Order\models\Order;
use app\modules\Order\models\OrderPrice;
use app\modules\Order\models\OrderPriceSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

/**
 * PriceController implements the CRUD actions for OrderPrice model.
 */
class PriceController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
		return [
			'verbs' => [
				'class' => VerbFilter::className(),
				'actions' => [
					'delete' => ['post'],
				],
			],
			'access' => [
				'class' => AccessControl::className(),
				'rules' => [
					[
						'allow' => true,
						'roles' => ['@'],
					],
				],
			],
		];
	}


    /**
     * Lists all OrderPrice models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new OrderPriceSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single OrderPrice model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new OrderPrice model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($order_id){//{{{
		$this->layout = "//empty";
        $model = new OrderPrice();

       	if ($model->load(Yii::$app->request->post()) && $model->save()) {

			// 变更订单状态
			$order = Order::findOne($model->order_id);
			Yii::$app->orderblock->order_status($order);
 
            return $this->redirect(['/order/order/view', 'id' => $model->order_id]);
        } else {
			$model->order_id = $order_id;

            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }//}}}

    /**
     * Updates an existing OrderPrice model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
		$this->layout = "//empty";
        $model = $this->findModel($id);

		//$model->load(Yii::$app->request->post());
		//var_dump($model->save());
		//var_dump($model->getErrors());
        if ($model->load(Yii::$app->request->post()) && $model->save()) {

			// 变更订单状态
			$order = Order::findOne($model->order_id);
			Yii::$app->orderblock->order_status($order);

            return $this->redirect(['/order/order/view', 'id' => $model->order_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing OrderPrice model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		$order_id = $model->order_id;	

		$model->delete();

		// 变更订单状态
		$order = Order::findOne($model->order_id);
		Yii::$app->orderblock->order_status($order);

        return $this->redirect(['/order/order/view', 'id'=>$order_id]);
        //return $this->redirect(['index']);
    }

    /**
     * Finds the OrderPrice model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return OrderPrice the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = OrderPrice::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
